<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入文章标题" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="发表时间" prop="created">
        <el-date-picker clearable v-model="queryParams.created" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择发表时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间" prop="modified">
        <el-date-picker clearable v-model="queryParams.modified" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="文章分类" prop="categories">
        <el-select v-model="queryParams.categories" placeholder="请选择文章分类" clearable>
          <el-option v-for="dict in category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="文章标签" prop="tags">
        <el-input v-model="queryParams.tags" placeholder="请输入文章标签" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div v-for="(article, index) in articleList" :key="article.id" class="property-item">
      <el-row justify="middle" align="middle" type="flex">
        <el-col :sm="24" :md="5">
          <image-preview :src="article.thumbnail" :width="150" :height="150" />
        </el-col>
        <el-col :sm="16" :md="11">
          <el-row align="middle">
            <el-col>
              <el-space :size="size">
                <span class="categorie-height" v-html="'发布于' + dateFormat(article.created, 'yyyy-MM-dd')"></span>
              </el-space>
            </el-col>
          </el-row>
          <el-row align="middle">
            <router-link :to="{ path: '/article_detail/' + article.id }" style="text-decoration: none;" class="title">
              <span class="title" v-html="article.title"></span>
            </router-link>
          </el-row>
          <el-row align="middle">
            <span v-html="article.content.substring(0, 90)"></span>
          </el-row>
        </el-col>
        <el-col :sm="4" :md="4" align="middle">
          <span class="highlight"> <strong> {{ article.statistic.hits }} </strong>次点击</span>
        </el-col>
        <el-col :sm="4" :md="4" align="middle">
          <span class="highlight"> <strong> {{ article.statistic.commentsNum }} </strong>条评论</span>
        </el-col>
      </el-row>
    </div>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="Article">
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from "@/api/show/article";
import { delComment } from "@/api/show/comment";

const { proxy } = getCurrentInstance();
const { comment_switch, category } = proxy.useDict('comment_switch', 'category');

const articleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    content: null,
    created: null,
    modified: null,
    categories: null,
    tags: null,
    allowComment: null,
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询文章操作列表 */
function getList() {
  loading.value = true;
  listArticle(queryParams.value).then(response => {
    articleList.value = response.list;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    created: null,
    modified: null,
    categories: null,
    tags: null,
    allowComment: null,
    thumbnail: null,
    statistic: {
      hits: null,
      commentsNum: null
    },
    comments: []
  };
  proxy.resetForm("articleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

import { format } from 'date-fns';

// 假设 dateFormat 是一个自定义函数，用于格式化日期
function dateFormat(dateString, formatString) {
  return format(new Date(dateString), formatString);
}

getList();
</script>
<style>
.categorie-height {
  line-height: 2;
  font-size: 15px;
  color: grey;
}

.title {
  color: rgb(10, 141, 255);
}
</style>